import 'package:family_expense/features/journey/models/journey_income_expense_detail.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

class JourneyIncomeExpenseDetailService {
  final String _url = '/journey-income-expense-detail';
  final HttpRequestService _httpRequestService = HttpRequestService();

  /// Retrieves current user account group all distinct years of income/expense from backend service.
  Future<List<int>> queryExistingDistinctYears() async {
    // Gets the response of the http get request.
    final response =
        await _httpRequestService.get('$_url/query-existing-distinct-years');

    // Parses the data from [Map<String, dynamic>] to int list.
    return List<int>.from(response.payload);
  }

  /// Retrieves current user account group [JourneyIncomeExpenseDetail] list from backend service.
  Future<List<JourneyIncomeExpenseDetail>> queryListByJourney(
      {required int pageSize,
      required int pageIndex,
      String journeyId = '',
      String keyword = ''}) async {
    // Gets the response of the http get request.
    final response =
        await _httpRequestService.get('$_url/query-list-by-journey', params: {
      'pageSize': pageSize,
      'pageIndex': pageIndex,
      'journeyId': journeyId,
      'keyword': keyword,
    });

    // Parses the data from [Map<String, dynamic>] to [JourneyIncomeExpenseDetail].
    return (response.payload as List<dynamic>)
        .map((p) => JourneyIncomeExpenseDetail.fromJson(p))
        .toList();
  }

  /// Retrieves current user account group [JourneyIncomeExpenseDetail] list from backend service.
  Future<JourneyIncomeExpenseDetail> queryDetails(String id) async {
    // Gets the response of the http get request.
    final response = await _httpRequestService
        .get('$_url/query-details', params: {'id': id});

    // Parses the data from [Map<String, dynamic>] to [JourneyIncomeExpenseDetail].
    return JourneyIncomeExpenseDetail.fromJson(response.payload);
  }

  /// Retrieves current user account group income/expense coordinates list from backend service.
  Future<List<JourneyIncomeExpenseDetail>> queryCoordinates(String year) async {
    // Gets the response of the http get request.
    final response = await _httpRequestService
        .get('$_url/query-coordinates', params: {'year': year});

    // Parses the data from [Map<String, dynamic>] to [JourneyIncomeExpenseDetail].
    return (response.payload as List<dynamic>)
        .map((p) => JourneyIncomeExpenseDetail.fromJson(p))
        .toList();
  }

  /// Adds the new [JourneyIncomeExpenseDetail] to server.
  Future<bool> add(
      JourneyIncomeExpenseDetail journeyincomeExpenseDetail) async {
    final response = await _httpRequestService.post(
      '$_url/add',
      body: journeyincomeExpenseDetail.toJson(),
    );

    return response.success;
  }

  /// Updates the new [JourneyIncomeExpenseDetail] to server.
  Future<bool> update(
      JourneyIncomeExpenseDetail journeyincomeExpenseDetail) async {
    final response = await _httpRequestService.post(
      '$_url/update',
      body: journeyincomeExpenseDetail.toJson(),
    );

    return response.success;
  }

  /// Updates the new [JourneyIncomeExpenseDetail] to server.
  Future<bool> delete(String? id) async {
    final response = await _httpRequestService.post(
      '$_url/delete',
      body: {'id': id},
    );

    return response.success;
  }
}
